package com.app.shop.controller.system;

import com.app.shop.util.ApiRest;
import com.app.shop.util.Constant;
import com.app.shop.util.SysModel;
import com.app.shop.entity.*;
import com.app.shop.entity.query.SysUserQuery;
import com.app.shop.config.security.DgbSecurityUserHelper;
import com.app.shop.service.system.SysAuthService;
import com.app.shop.service.system.SysReAuthUserService;
import com.app.shop.service.system.SysUserService;
import com.app.shop.entity.show.BaseShow;
import com.app.shop.entity.show.SysUserShow;
import com.app.shop.util.ClassUtil;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.Page;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * 首页数据展示处理Controller模块
 *
 * @author chenliwei
 * @version v1.0 2017-11-5
 */
@Controller
@RequestMapping(value = "/sysUser")
public class UserController extends BaseController {
    private static final Logger log = LoggerFactory.getLogger(UserController.class);


    @Autowired
    SysUserService sysUserService;

    @Autowired
    SysAuthService sysAuthService;

    @Autowired
    SysReAuthUserService sysReAuthUserService;

    /**
     * 查询用户列表
     *
     * @param sysUserQuery
     * @return
     * @throws IOException
     */
    @ResponseBody
    @RequestMapping(value = "/getUserList")
    public ApiRest getDictList(@RequestBody SysUserQuery sysUserQuery) {
        log.debug(JSONObject.toJSON(sysUserQuery).toString());
        //模拟分页查询
        Page<SysUser> sysUserPage = sysUserService.getSysUserList(sysUserQuery);
        //查询结果对象置换
        List<SysUserShow> resList = new ArrayList<SysUserShow>();

        SysUserShow sysUserShow;
        //数据转换
        for (SysUser sysUser : sysUserPage.getResult()) {
            sysUserShow = new SysUserShow();
            try {
                ClassUtil.fatherToChild(sysUser, sysUserShow);
            } catch (Exception e) {
                e.printStackTrace();
            }
            //码表值补全
            sysUserShow.dataSet();
            resList.add(sysUserShow);

        }
        return new ApiRest(Constant.REST_SUCCESS_CODE, Constant.SUCCESS_MESSAGE,
                new BaseShow(sysUserPage.getPageNum(), sysUserPage.getPageSize(), sysUserPage.getTotal(), sysUserPage.getPages(), resList));
    }

    /**
     * 重置用户密码（默认密码123456）
     *
     * @param sysUserQuery 需要重置的用户信息
     * @return 处理结果
     */
    @ResponseBody
    @RequestMapping(value = "resetUserPwd")
    public ApiRest resetUserPwd(@RequestBody SysUserQuery sysUserQuery) {
        log.debug(JSONObject.toJSON(sysUserQuery).toString());
        sysUserQuery.setOptionUserId(DgbSecurityUserHelper.getUserInfo().getUserId());
        //重置用户密码
        sysUserService.resetUserPwd(sysUserQuery);
        return new ApiRest(Constant.REST_SUCCESS_CODE, Constant.SUCCESS_MESSAGE, null);
    }

    /**
     * 修改用户密码（默认密码123456）
     *
     * @param sysUserQuery 需要重置的用户信息
     * @return 处理结果
     */
    @ResponseBody
    @RequestMapping(value = "editUserPwd")
    public ApiRest editUserPwd(@RequestBody SysUserQuery sysUserQuery) {
        log.debug(JSONObject.toJSON(sysUserQuery).toString());
        //重置用户密码
        sysUserQuery.setOptionUserId(DgbSecurityUserHelper.getUserInfo().getUserId());
        SysModel sysModel = sysUserService.editUserPwd(sysUserQuery);
        if (sysModel.isSuccess()) {
            return new ApiRest(Constant.REST_SUCCESS_CODE, Constant.SUCCESS_MESSAGE, null);
        } else {
            return new ApiRest(Constant.REST_FAIL_CODE, sysModel.getMsg(), null);
        }
    }

    /**
     * 用户启用
     *
     * @param sysUserQuery 需要启用的用户信息
     * @return 需要重置的用户信息
     */
    @ResponseBody
    @RequestMapping(value = "updateUserStart")
    public ApiRest resetUserStart(@RequestBody SysUserQuery sysUserQuery) {
        log.debug(JSONObject.toJSON(sysUserQuery).toString());
        sysUserQuery.setOptionUserId(DgbSecurityUserHelper.getUserInfo().getUserId());
        sysUserService.updateUserStart(sysUserQuery);
        return new ApiRest(Constant.REST_SUCCESS_CODE, Constant.SUCCESS_MESSAGE, null);
    }

    /**
     * 用户禁用
     *
     * @param sysUserQuery 需要禁用的用户信息
     * @return 处理结果
     */
    @ResponseBody
    @RequestMapping(value = "updateUserStop")
    public ApiRest updateUserStop(@RequestBody SysUserQuery sysUserQuery) {
        log.debug(JSONObject.toJSON(sysUserQuery).toString());
        sysUserQuery.setOptionUserId(DgbSecurityUserHelper.getUserInfo().getUserId());
        sysUserService.updateUserStop(sysUserQuery);
        return new ApiRest(Constant.REST_SUCCESS_CODE, Constant.SUCCESS_MESSAGE, null);
    }

    /**
     * 用户信息查询
     *
     * @param sysUserQuery 需要查询的用户信息
     * @return 用户信息
     */
    @ResponseBody
    @RequestMapping(value = "getUserInfo")
    public ApiRest getUserInfo(@RequestBody SysUserQuery sysUserQuery) {
        log.debug(JSONObject.toJSON(sysUserQuery).toString());
        SysUserShow sysUserShow = sysUserService.getUserInfo(sysUserQuery);
        //获取用户权限关系
        List<SysReAuthUser> sysReAuthUserList = sysReAuthUserService.getSysAuthListByUserId(sysUserQuery.getUserId());
        //获取权限列表
        List<SysAuth> sysUserListAll = sysAuthService.getSysAuthListAll();
        StringBuffer sb = new StringBuffer();
        for (SysReAuthUser sysReAuthUser : sysReAuthUserList) {
            for (SysAuth sysAuth : sysUserListAll) {
                {
                    if (sysReAuthUser.getAuthId().equals(sysAuth.getAuthId())) {
                        sb = sb.length() > 0 ? sb.append("," + sysAuth.getAuthName()) : sb.append(sysAuth.getAuthName());
                    }
                }
            }
        }
        sysUserShow.setAuthNames(sb.toString());
        return new ApiRest(Constant.REST_SUCCESS_CODE, Constant.SUCCESS_MESSAGE, sysUserShow);
    }


    /**
     * 用户信息添加
     *
     * @param sysUser 需要添加的用户信息
     * @return 处理结果
     */
    @ResponseBody
    @RequestMapping(value = "addUserInfo")
    public ApiRest addUserInfo(@RequestBody SysUser sysUser) {
        log.debug(JSONObject.toJSON(sysUser).toString());
        sysUser.setCreateUserId(DgbSecurityUserHelper.getUserInfo().getUserId());
        sysUser.setUpdateUserId(sysUser.getCreateUserId());

        SysModel sysModel = sysUserService.addUserInfo(sysUser);
        if (sysModel.isSuccess()) {
            return new ApiRest(Constant.REST_SUCCESS_CODE, Constant.SUCCESS_MESSAGE, null);
        } else {
            return new ApiRest(Constant.REST_FAIL_CODE, sysModel.getMsg(), null);
        }
    }

    /**
     * 用户信息修改
     *
     * @param sysUser 需要修改的用户信息
     * @return 处理结果
     */
    @ResponseBody
    @RequestMapping(value = "updateUserInfo")
    public ApiRest updateUserInfo(@RequestBody SysUser sysUser) {
        log.debug(JSONObject.toJSON(sysUser).toString());
        sysUser.setUpdateUserId(DgbSecurityUserHelper.getUserInfo().getUserId());
        SysModel sysModel = sysUserService.updateUserInfo(sysUser);
        if (sysModel.isSuccess()) {
            return new ApiRest(Constant.REST_SUCCESS_CODE, Constant.SUCCESS_MESSAGE, null);
        } else {
            return new ApiRest(Constant.REST_FAIL_CODE, sysModel.getMsg(), null);
        }
    }

    /**
     * 根据用户ID查询用户权限信息
     *
     * @param sysUserQuery 需要查询用户权限信息
     * @return 用户权限信息
     */
    @ResponseBody
    @RequestMapping(value = "getAuthUserInfo")
    public ApiRest getAuthUserInfo(@RequestBody SysUserQuery sysUserQuery) {
        log.debug(JSONObject.toJSON(sysUserQuery).toString());
        //获取用户权限关系
        List<SysReAuthUser> sysReAuthUserList = sysReAuthUserService.getSysAuthListByUserId(sysUserQuery.getUserId());
        //获取权限列表
        List<SysAuth> sysListAll = sysAuthService.getSysAuthListAll();
        Map map = new HashMap();
        map.put("checkedAuths", sysReAuthUserList);
        map.put("authOptions", sysListAll);
        return new ApiRest(Constant.REST_SUCCESS_CODE, Constant.SUCCESS_MESSAGE, map);
    }


    /**
     * 根据用户ID更新相关权限信息
     *
     * @param sysUserQuery 需要修改的参数数据
     * @return 处理结果
     */
    @ResponseBody
    @RequestMapping(value = "updateAuthUserInfo")
    public ApiRest updateAuthUserInfo(@RequestBody SysUserQuery sysUserQuery) {
        log.debug(JSONObject.toJSON(sysUserQuery).toString());
        boolean bool;
        sysUserQuery.setOptionUserId(DgbSecurityUserHelper.getUserInfo().getUserId());
        SysModel sysModel = sysReAuthUserService.updateAuthUserInfo(sysUserQuery);

        if (sysModel.isSuccess()) {
            return new ApiRest(Constant.REST_SUCCESS_CODE, Constant.SUCCESS_MESSAGE, null);
        } else {
            return new ApiRest(Constant.REST_FAIL_CODE, sysModel.getMsg(), null);
        }
    }
}